package com.swh.dao;

import com.swh.entity.Dept;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.List;
import java.util.Map;

/**
 * @Author: swh
 * @Description:
 * @Date: 2020/11/23 16:59
 * @Version: 1.0
 */
public class DeptDao extends BaseDao {

    public List<Map<String, Object>> findAll() {
        String sql = "select d.*,count(u.dept_id) deptCount from dept d " +
                " left join user u on d.id=u.dept_id group by d.id";
        return jdbcTemplate.queryForList(sql);
    }


    public Dept findById(Long deptId) {
        try {
            return jdbcTemplate.queryForObject("select * from dept where id=?",
                    new BeanPropertyRowMapper<>(Dept.class),deptId);
        } catch (EmptyResultDataAccessException e) {
            return null;
        }

    }

    public List<Dept> findDeptAll() {
        return jdbcTemplate.query("select * from dept",new BeanPropertyRowMapper<>(Dept.class));
    }
}
